﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using System.Transactions;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class MenuCategoryDAO : Repository<Model.Entities.Model_MenuCategory>, IMenuCategoryDAO
    {
        public override bool Add(Model.Entities.Model_MenuCategory entity)
        {
            try
            {
                entity.IsModelBaseNullThrowException();
                return base.Add(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        public override bool Delete(Model.Entities.Model_MenuCategory entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_MenuCategory menuCategory = GetByID(id);
            return Delete(menuCategory);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from m in context.MenuCategories
                                  where listID.Contains(m.ID) && m.IsUse == true && m.IsDelete == false
                                  select m;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_MenuCategory>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) != null ? true : false;
        }
        /// <summary>
        /// GetByID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override Model.Entities.Model_MenuCategory GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                Model.Entities.Model_MenuCategory model_MenuCategory = context.MenuCategories.FirstOrDefault(m => m.ID == id && m.IsUse == true && m.IsDelete == false);
                return model_MenuCategory;
            }
        }
        public override IList<Model.Entities.Model_MenuCategory> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_MenuCategory entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }
        /// <summary>
        /// 获取所有的菜单类别
        /// </summary>
        /// <returns></returns>
        public IList<Model.Entities.Model_MenuCategory> GetAll()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuCategories.ToList();
            }
        }


        public Model.Entities.Model_MenuCategory GetByName(string name)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuCategories.Where(m => m.IsUse == true && m.IsDelete == false && m.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            }
        }
    }
}
